package com.ldatum.crm.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ldatum.crm.dao.TCustomerDao;
import com.ldatum.crm.domain.TContacts;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.util.HibernateUtil;

public class TCustomerDaoImpl implements TCustomerDao {

	@Override
	public void add(TCustomer customer) throws Exception{
		
		HibernateUtil.add(customer);
	}

	@Override
	public void delete(TCustomer customer, int id) throws Exception {
		HibernateUtil.delete(customer);
	}

	@Override
	public void update(TCustomer customer, int id) throws Exception {
		HibernateUtil.update(customer);
	}

	@Override
	public TCustomer findById(Class<TCustomer> clazz, int id)  throws Exception{
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			TCustomer customer = (TCustomer) s.get(clazz, id);
			return customer;
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public List<TCustomer> myCustomer(Class<TCustomer> clazz, String proprietor) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TCustomer as customer where proprietor =:proprietor or creater = :creater order by buildDate DESC";
			Query q = s.createQuery(hql);
			q.setString("proprietor", proprietor);
			q.setString("creater", proprietor);
			@SuppressWarnings("unchecked")
			List<TCustomer> list = (List<TCustomer>) q.list();
			
			return list;
		}finally{
			s.close();
		}
	}

	@Override
	public String generateNo(String currentDate) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "select max(number) as no from TCustomer as customer where number like '%"+currentDate+"%'";
			Query q = s.createQuery(hql);
			String maxNo = (String) q.uniqueResult();//查找出的最大的客户编号
			String no = "";//要返回的客户编号
			if(maxNo==null){
				no = "KH"+currentDate+"001";
			}else{
				int suffix = Integer.parseInt(maxNo.substring(maxNo.length()-4, maxNo.length()));
				suffix = suffix + 1;
				no = "KH"+currentDate.substring(0, currentDate.length()-1)+suffix;
			}
			return no;
		}finally{
			s.close();
		}
	}

	@Override
	public void deletes(TCustomer customer, String ids) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			String hql = "delete from TCustomer as customer where id in ("+ids+")";
			Query q = s.createQuery(hql);
			q.executeUpdate();
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public List<TCustomer> publicCustomer(Class<TCustomer> class1)
			throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TCustomer as customer where isPublic =:isPublic order by buildDate DESC";
			Query q = s.createQuery(hql);
			q.setString("isPublic", "1");
			@SuppressWarnings("unchecked")
			List<TCustomer> list = (List<TCustomer>) q.list();
			return list;
		}finally{
			s.close();
		}
	}

	@Override
	public List<TCustomer> sharedCustomer(Class<TCustomer> class1)
			throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TCustomer as customer where isShared =:isShared order by buildDate DESC";
			Query q = s.createQuery(hql);
			q.setString("isShared", "1");
			@SuppressWarnings("unchecked")
			List<TCustomer> list = (List<TCustomer>) q.list();
			return list;
		}finally{
			s.close();
		}
	}

	@Override
	public void save(TCustomer customer, TContacts contact) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			contact.setCustomer(customer);
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(customer);
			s.save(contact);
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}
}
